const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({
  data: {
    adresslist:[],
    adress:'',
    sexlist:['男','女'],
    nums:2,
    info:[
      {name:'',sex:'',mobile:'',idcard:'',remark:''},
      {name:'',sex:'',mobile:'',idcard:'',remark:''},
      {name:'',sex:'',mobile:'',idcard:'',remark:''},
    ],
    isagree:false,
    istips:false,
    bottomLift: 0,
    id:'',
    no:'',
    price:0,
    ticket:[],
    ticket1:[],
    detail:'',
    time:'',
    currdentonfo:'',
    totalAmount:0,
    totalPreAmount:0,
  },
  onLoad: function (option) {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }

    if(option.id){
      this.setData({
        id:option.id,
        no:option.no,
        price:option.price,
        ticket:app.globalData.ticket,
        ticket1:app.globalData.ticket,
        time:option.time
      })
      this.activitydetail()
    }
  },
  activitydetail(){
    Controller.activitydetail({activityId:this.data.id}).then(res=>{
      if(res.data.baseData){
        res.data.baseData.meetPlace=res.data.baseData.meetPlace.split('&');
        this.setData({
          detail:res.data.baseData
        })
        var ticket=this.data.ticket;
        var lists=[]
        for(var i=0;i<ticket.length;i++){
          var item = ticket[i];
          
          for(var j=0;j<ticket[i].nums;j++){
            var item1=JSON.parse(JSON.stringify(item));
            item1.name='';
            item1.sex='';
            item1.mobile='';
            item1.idcard='';
            item1.remark='';
            item1.index=j+1;
            lists.push(item1);
          }
          
        }
        this.setData({
          ticket:lists
        })
        this.orderItemList()
        this.totalOrderItemAmount()
      }
    })
  },
  pickeradress: function(e) {
    this.setData({
      adress:this.data.detail.meetPlace[e.detail.value]
    })
  },
  pickersex: function(e) {
    var index = e.currentTarget.dataset.index;
    var name = e.currentTarget.dataset.name;
    var info=this.data.ticket;
    for(var i=0;i<info.length;i++){
      if(index==i&&info[i].type!==2){
        info[i][name]=this.data.sexlist[e.detail.value]
      }
    }
    this.setData({
      ticket:info,
    })
  },
  changeinput(e){
    var index = e.currentTarget.dataset.index;
    var name = e.currentTarget.dataset.name;
    var ticket=this.data.ticket;
    for(var i=0;i<ticket.length;i++){
      if(index==i){
        ticket[i][name]=e.detail.value;
        break;
      }
    }
    console.log(ticket)
    this.setData({
      ticket,
    })
  },
  agree(){
    this.setData({
      isagree:!this.data.isagree
    })
  },
  goagree(){
    wx.navigateTo({
      url:"/pages/doc/doc?key=pay-agreement"
    });
  },
  orderItemList(){
    Controller.orderItemList({orderNumber:this.data.no}).then(res=>{
      if(res.data.baseData){
        this.setData({
          currdentonfo:res.data.baseData,
        })
      }
    })
  },
  totalOrderItemAmount(){
    Controller.totalOrderItemAmount({orderNumber:this.data.no}).then(res=>{
      if(res.data.baseData){
        this.setData({
          totalAmount:res.data.baseData.totalAmount,
          totalPreAmount:res.data.baseData.totalPreAmount,
        })
      }
    })
  },
  tips(){
    this.setData({
      istips:true
    })
  },
  close(){
    this.setData({
      istips:false
    })
  },
  buy(){
    if(!this.data.adress){
      wx.showToast({
        title: '请选择您的集合地点',
        icon: 'none',
        duration: 1200
      });
      return
    }
    var info=this.data.ticket;
    for(var i=0;i<info.length;i++){
        if(!info[i].name&&info[i].type==1){
          wx.showToast({
            title: info[i].title+'报名信息+'+(info[i].index)+'的名字须与身份证名字一致',
            icon: 'none',
            duration: 1200
          });
          return
        }
        if(!info[i].sex&&info[i].type==1){
          wx.showToast({
            title: info[i].title+'报名信息+'+(info[i].index)+'的性别须与身份证性别一致',
            icon: 'none',
            duration: 1200
          });
          return
        }
        if(!info[i].mobile&&info[i].type==1){
          wx.showToast({
            title: '请输入+'+info[i].title+'报名信息+'+(info[i].index)+'的手机号码',
            icon: 'none',
            duration: 1200
          });
          return
        }
        if (!(/^1[123456789]\d{9}$/.test(info[i].mobile))&&info[i].type==1) {
          wx.showToast({
            title: info[i].title+'报名信息+'+(info[i].index)+'的手机号码错误',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
        if(!info[i].idcard&&info[i].type==1){
          wx.showToast({
            title: '请输入+'+info[i].title+'报名信息+'+(info[i].index)+'的身份证信息',
            icon: 'none',
            duration: 1200
          });
          return
        }
        if (!util.idCardNumber(info[i].idcard)&&info[i].type==1){
          wx.showToast({
            title: info[i].title+'报名信息+'+(info[i].index)+'的身份证信息错误',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
    }
    if(!this.data.isagree){
      wx.showToast({
        title: '阅读并同意《支付协议》',
        icon: 'none',
        duration: 1200
      });
      return
    }

    var lists=[];
    for(var i=0;i<info.length;i++){
      if(info[i].nums>0&&info[i].type==1){
        lists.push({name:info[i].name,sex:info[i].sex=="男"?1:2,mobile:info[i].mobile,idCardNumber:info[i].idcard,remark:info[i].remark});
      }
    }
    var data={
      orderNumber:this.data.no,
      meetPlace:this.data.adress,
      signInfoBody:JSON.stringify(lists),
      parentId:app.globalData.parentid || ''
    }
    Controller.updateOrder(data).then(res=>{
      if(res.data.baseData){
        this.setData({
          no:res.data.baseData
        })
        this.pay()
      }
    })
  },
  pay(e){
    Controller.pay({orderNumber:this.data.no, payType:1}).then(res => {
      let data=res.data.baseData;
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res){
          setTimeout(() => {
            wx.switchTab({
              url: "/pages/order/index"
            })
          },1000)
          wx.showToast({
            title: "支付成功",
            icon: 'none',
            duration: 1000
          });
        },
        fail(res){
          setTimeout(() => {
            wx.switchTab({
              url: "/pages/order/index"
            })
          },1000)
          wx.showToast({
            title: "支付失败",
            icon: 'none',
            duration: 1000
          });
        }
      })
    }).catch(res=>{
      setTimeout(() => {
        wx.switchTab({
          url: "/pages/order/index"
        })
      },1000)
      wx.showToast({
        title: "支付失败",
        icon: 'none',
        duration: 1000
      });
    })
  },
  onShareAppMessage: function () {

  }
})